#region Developement by Visualizer
#endregion 

using System;
using System.Collections;
using System.Drawing;

using Gizmox.WebGUI.Common.Resources;
using Gizmox.WebGUI.Forms;

namespace DragDrop
{
    public partial class BaseForm : Form
    {
        #region members

        #endregion members

        #region properties
        private int xOffset
        {
            get
            {
                return pnlLeft.DockPadding.Left +
                       pnlToolbox.Width +
                       pnlLeft.DockPadding.Right +
                       splitter1.Width +
                       pnlForm.DockPadding.Left;
            }
        }
        private int yOffset
        {
            get
            {
                return pnlFill.DockPadding.Top;
            }
        }
        #endregion properties

        #region constructor
        public BaseForm()
        {
            InitializeComponent();
        }
        #endregion constructor

        #region event handlers
        private void BaseForm_Load(object sender, EventArgs e)
        {
            InitForm();
        }
        private void pnlForm_DragDrop(object sender, DragEventArgs e)
        {
            DragDropEventArgs dragDropEventArgs = e as DragDropEventArgs;

            //move existing
            if(((Control)dragDropEventArgs.Source).Parent.Name.ToUpper() == "PNLFORM")
            {
                MoveExistingControl(dragDropEventArgs, e);
            }
            //create new
            else
            {
                CreateNewControl(dragDropEventArgs, e);
            }

           
        }
        private void control_Click(object sender, EventArgs e)
        {
            pg.SelectedObject = sender;
        }
        #endregion event handlers

        #region methods
        private void InitForm()
        {
            SetToolBoxDragTragets();
        }

        private void SetToolBoxDragTragets()
        {
            SetDragTragets(lblToolbox);
            SetDragTragets(pnlTextBox);
            SetDragTragets(pbToolbox);
        }
        private void SetDragTragets(Control control)
        {
            ArrayList dragTargets = new ArrayList();
            dragTargets.Add(pnlForm);
            pnlForm.AllowDrop = true;

            control.DragTargets = (Gizmox.WebGUI.Forms.Component[])dragTargets.ToArray(typeof(Gizmox.WebGUI.Forms.Component));
        }

        private void MoveExistingControl(DragDropEventArgs dragDropEventArgs, DragEventArgs e)
        {
            switch (dragDropEventArgs.Source.GetType().Name.ToUpper())
            {
                case "PICTUREBOX":
                    {
                        PictureBox pb = (PictureBox) dragDropEventArgs.Source;
                        pb.Location = new Point(e.X - xOffset, e.Y - yOffset);
                        break;
                    }
                case "LABEL":
                    {
                        Label lbl = (Label)dragDropEventArgs.Source;
                        lbl.Location = new Point(e.X - xOffset, e.Y - yOffset);
                        break;
                    }
                case "TEXTBOX":
                    {
                        TextBox tb = (TextBox)dragDropEventArgs.Source;
                        tb.Location = new Point(e.X - xOffset, e.Y - yOffset);
                        break;
                    }
            }
        }
        private void CreateNewControl(DragDropEventArgs dragDropEventArgs, DragEventArgs e)
        {
            switch (dragDropEventArgs.Source.GetType().Name.ToUpper())
            {
                case "PICTUREBOX":
                    {
                        PictureBox pb = new PictureBox
                        {
                            Size = new Size(pbToolbox.Width, pbToolbox.Height),
                            Location = new Point(e.X - xOffset, e.Y - yOffset),
                            Image = new IconResourceHandle("32x32_workplace.png")
                        };
                        pnlForm.Controls.Add(pb);
                        pb.Click += new EventHandler(control_Click);
                        SetDragTragets(pb);
                        pg.SelectedObject = pb;
                        break;
                    }
                case "LABEL":
                    {
                        Label lbl = new Label
                        {
                            AutoSize = true,
                            Location = new Point(e.X - xOffset, e.Y - yOffset),
                            Text = lblToolbox.Text
                        };
                        pnlForm.Controls.Add(lbl);
                        lbl.Click += new EventHandler(control_Click);
                        SetDragTragets(lbl);
                        pg.SelectedObject = lbl;
                        break;
                    }
                case "PANEL":
                    {
                        TextBox tb = new TextBox
                        {
                            Size = new Size(pnlTextBox.Width, pnlTextBox.Height),
                            Location = new Point(e.X - xOffset, e.Y - yOffset),
                        };
                        pnlForm.Controls.Add(tb);
                        tb.Click += new EventHandler(control_Click);
                        SetDragTragets(tb);
                        pg.SelectedObject = tb;
                        break;
                    }
            }
        }

        #endregion methods
    }
}